package com.major.dawn.leetCode.string;

/**
 * 回文串，回文结构的判断
 */
public class PalindromeString {

    private int head = 0;

    private int tail;

//    头尾双指针，判断相遇
    public boolean isPalindrome(String s) {
        if (s == null) {
            return false;
        }
        s = s.replaceAll("[^0-9a-zA-Z]", "").toLowerCase();

        if (s.equals("")) {
            return true;
        }
        char[] chars = s.toCharArray();
        tail = chars.length -1 ;
//        奇偶数判断
        while (!(tail == head - 1 || head == tail)) {
            if (chars[head] != chars[tail]) {
                return false;
            }
            head++;
            tail--;
        }

        return true;
    }




    public static void main(String[] args) {

        PalindromeString palindromeString = new PalindromeString();
        boolean palindrome = palindromeString.isPalindrome("A man, a plan, a canal: Panama");
        System.out.println(palindrome);

    }

}
